package com.android.corestat.utils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2014年4月21日
 * @version 1.0
 */

public class ApkUtils {
	public static Set<String> getAllPackageNames(Context context) {
		HashSet<String> set = new HashSet<String>();
		List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);

		for (int i = 0; i < packages.size(); i++) {
			PackageInfo pkg = packages.get(i);
			set.add(pkg.packageName);
		}
		return set;
	}

	public static String getApkPath(Context context, String name) {
		PackageManager pm = context.getPackageManager();

		try {
			ApplicationInfo ai = pm.getApplicationInfo(name, 0);
			return ai.publicSourceDir;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 判断apk是否是系统应用
	public static boolean packageIsSystemApk(Context context, String packageName) {
		PackageManager pm = context.getPackageManager();
		try {
			ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
            return (ai.flags & ApplicationInfo.FLAG_SYSTEM) > 0;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		return false;
	}
}
